// pages/components/silderNumber/sliderNumber.js
Component({
	/**
	 * 组件的属性列表
	 */
	properties: {
		targetNumber:{
			type: Number,
			value:0,
		},
		animationTime:{
			type: Number,
			value:1000,
		},
		color:{
			type: String,
			value:"",
		}
	},
	data: {
		step:0.01,
		timer :null,
		numbers:[],

		per_num:0
	},
	observers:{
		"targetNumber":function(numberA, numberB){
			console.log(numberA, numberB  ,this.data.per_num)
			if( this.data.per_num==0 ){
				this.data.per_num = numberA;
				this.setData({ per_num:this.data.per_num })
			}else{
				this.startAnimation()
			}
		} 
	},
	pageLifetimes: {
		hide: function () {
			clearInterval(this.data.timer)
		},
	},
	methods: {
		startAnimation:function(){
			this.change()
		},
		change:function(){
			clearInterval(this.data.timer)
			if( this.data.targetNumber==this.data.per_num ) return;
			this.data.timer = setInterval(()=>{
				let num = this.data.per_num;
				if( Number(num)<Number(this.data.targetNumber) ){
					num = Number(num) + Number(this.data.step);
				}
				if( Number(num)>Number(this.data.targetNumber) ){
					num = Number(num) - Number(this.data.step);
				}
				this.data.per_num = (num||"").substr(0,4);
				this.setData({
					per_num:this.data.per_num
				})
				if( this.data.per_num>= this.data.targetNumber){
					clearInterval(this.data.timer)
					return
				}
			},100)
		},
	}
})
